﻿package mortal.game.view.lookPlayerInfo
{
    import Message.Client.*;
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import extend.language.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.mvc.core.*;

    public class LookPlayerSystemController extends Controller
    {
        private var _windowList:Vector.<LookPlayerSystemModule>;
        private var _currentWinList:Vector.<LookPlayerSystemModule>;

        public function LookPlayerSystemController()
        {
            this._windowList = new Vector.<LookPlayerSystemModule>;
            this._currentWinList = new Vector.<LookPlayerSystemModule>;
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.LookPlayerModuleOpen, this.openLookPlayerInfoWin);
            NetDispatcher.addCmdListener(ServerCommand.LookMountInfoUpdate, this.updateOtherInfo);
            Dispatcher.addEventListener(EventName.LookPlayerInfo, this.getPlayerInfo);
            Dispatcher.addEventListener(EventName.LookPlayerModuleClosed, this.hideWin);
            Dispatcher.addEventListener(EventName.LookPlayerInfoByType, this.getOtherSystemInfoByType);
            return;
        }// end function

        private function updateOtherInfo(param1:SLookMountReply) : void
        {
            var _loc_2:* = this.getWinByEntityId(param1.ownEntityId);
            if (_loc_2)
            {
                _loc_2.updateOtherSysteamInfo(param1.mountInfo);
            }
            return;
        }// end function

        private function getWinByEntityId(param1:SEntityId) : LookPlayerSystemModule
        {
            var _loc_2:SEntityId = null;
            var _loc_3:LookPlayerSystemModule = null;
            for each (_loc_3 in this._currentWinList)
            {
                
                _loc_2 = _loc_3.slookPlayerInfo.miniPlayer.entityId;
                if (EntityUtil.equal(_loc_2, param1))
                {
                    return _loc_3;
                }
            }
            return null;
        }// end function

        private function getModule() : LookPlayerSystemModule
        {
            var _loc_1:LookPlayerSystemModule = null;
            if (this._windowList.length)
            {
                _loc_1 = this._windowList.pop();
            }
            else
            {
                _loc_1 = new LookPlayerSystemModule();
            }
            return _loc_1;
        }// end function

        private function openLookPlayerInfoWin(param1:SLookPlayerInfo) : void
        {
            var _loc_2:LookPlayerSystemModule = null;
            if (this._currentWinList.length == 2)
            {
                this._currentWinList[0].show();
                this._currentWinList[0].updatePlayerInfo(param1);
                this._currentWinList.reverse();
            }
            else
            {
                _loc_2 = this.getModule();
                _loc_2.show();
                _loc_2.updatePlayerInfo(param1);
                this._currentWinList.push(_loc_2);
            }
            return;
        }// end function

        private function getPlayerInfo(event:DataEvent) : void
        {
            var _loc_2:SEntityId = null;
            var _loc_3:BossInfo = null;
            var _loc_4:TRobot = null;
            var _loc_5:Array = null;
            var _loc_6:int = 0;
            var _loc_7:SLookPlayerInfo = null;
            var _loc_8:LookPlayerSystemModule = null;
            var _loc_9:int = 0;
            if (event.data is SEntityId)
            {
                _loc_2 = event.data as SEntityId;
            }
            else if (event.data is SMiniPlayer)
            {
                _loc_2 = (event.data as SMiniPlayer).entityId;
                if (event.data is MiniPlayerInfo)
                {
                    _loc_3 = BossConfig.instance.getInfoByCode((event.data as MiniPlayerInfo).robotId);
                }
                else if (_loc_2.id == 0)
                {
                    _loc_5 = [33000001, 33000002, 33000003];
                    for each (_loc_6 in _loc_5)
                    {
                        
                        _loc_3 = BossConfig.instance.getInfoByCode(_loc_6);
                        if (_loc_3)
                        {
                            _loc_4 = RobotConfig.instance.getRobotByCode(_loc_3.code);
                            if (_loc_4 && _loc_4.career == (event.data as SMiniPlayer).career)
                            {
                                break;
                                continue;
                            }
                            _loc_3 = null;
                        }
                    }
                }
            }
            if (_loc_3)
            {
                if (_loc_2.id == 0)
                {
                    _loc_4 = RobotConfig.instance.getRobotByCode(_loc_3.code);
                    if (_loc_4)
                    {
                        _loc_7 = this.changeTRobotToLookPlayerInfo(_loc_4, _loc_3, _loc_2, event.data as SMiniPlayer);
                        NetDispatcher.dispatchCmd(ServerCommand.LookPlayerModuleOpen, _loc_7);
                    }
                    else
                    {
                        return MsgManager.showRollTipsMsg(Language.getString(35341), MsgRollTipsType.msgRollTips3);
                    }
                }
                else
                {
                    return MsgManager.showRollTipsMsg(Language.getString(35341), MsgRollTipsType.msgRollTips3);
                }
            }
            else
            {
                for each (_loc_8 in this._currentWinList)
                {
                    
                    if (_loc_8.slookPlayerInfo && EntityUtil.equal(_loc_8.slookPlayerInfo.miniPlayer.entityId, _loc_2))
                    {
                        return MsgManager.showRollTipsMsg(Language.getString(35342));
                    }
                }
                _loc_9 = ELookUpType._ELookUpTypePlayer;
                GameProxy.chat.lookupRequest(ELookRoleInfoType._ECommonRoleInfo, _loc_2, _loc_9);
            }
            return;
        }// end function

        private function getOtherSystemInfoByType(event:DataEvent) : void
        {
            var _loc_4:SLookMountReply = null;
            var _loc_2:* = event.data.id;
            var _loc_3:* = event.data.type as int;
            if (_loc_2.id == 0)
            {
                _loc_4 = new SLookMountReply();
                _loc_4.ownEntityId = _loc_2;
                _loc_4.type = _loc_3;
                _loc_4.mountInfo = new SLookPlayerMountInfo();
                _loc_4.mountInfo.code = 1;
                _loc_4.mountInfo.playerItems = [];
                _loc_4.mountInfo.skillIds = [];
                NetDispatcher.dispatchCmd(ServerCommand.LookMountInfoUpdate, _loc_4);
            }
            else
            {
                GameProxy.chat.lookupRequest(ELookRoleInfoType._ECommonRoleInfo, _loc_2, _loc_3);
            }
            return;
        }// end function

        private function changeTRobotToLookPlayerInfo(param1:TRobot, param2:TBoss, param3:SEntityId, param4:SMiniPlayer) : SLookPlayerInfo
        {
            var _loc_13:String = null;
            var _loc_14:Object = null;
            var _loc_15:SPlayerItem = null;
            var _loc_5:* = JSON.parse(param1.extendStr);
            var _loc_6:* = _loc_5.Attr;
            var _loc_7:* = _loc_5.equip;
            var _loc_8:* = new SLookPlayerInfo();
            var _loc_9:* = new SFightAttribute();
            var _loc_10:* = Cache.instance.entity.getEntityInfoById(param3);
            var _loc_11:Array = [];
            _loc_8.miniPlayer = param4;
            _loc_8.cellFight = _loc_9;
            _loc_8.playerItems = _loc_11;
            if (_loc_10)
            {
                _loc_8.life = _loc_10.entityInfo.life;
                _loc_8.mana = _loc_10.entityInfo.mana;
                param4.name = _loc_10.entityInfo.name;
            }
            else
            {
                _loc_8.life = param2.maxLife;
                _loc_8.mana = param2.maxMana;
            }
            param4.career = param1.career;
            param4.signature = "";
            if (param4.level < GameDefConfig.instance.getSystemOpenLevel(63))
            {
                _loc_8.clothesCode = 0;
                _loc_8.weaponCode = 0;
            }
            else
            {
                _loc_8.clothesCode = 1;
                _loc_8.weaponCode = 1;
            }
            var _loc_12:Array = ["attack", "maxLife", "maxMana", "physDefense", "magicDefense", "penetration", "jouk", "hit", "crit", "toughness", "block", "expertise"];
            for each (_loc_13 in _loc_12)
            {
                
                if (param2.hasOwnProperty(_loc_13))
                {
                    _loc_9[_loc_13] = param2[_loc_13];
                }
            }
            for each (_loc_14 in _loc_7)
            {
                
                _loc_15 = new SPlayerItem();
                _loc_15.itemCode = _loc_14.code;
                _loc_15.jsStr = JSON.stringify(_loc_14.js);
                _loc_15.itemAmount = _loc_14.amount;
                _loc_15.uid = _loc_14.uid;
                _loc_11.push(_loc_15);
            }
            return _loc_8;
        }// end function

        private function hideWin(event:DataEvent) : void
        {
            var _loc_2:* = event.data as LookPlayerSystemModule;
            var _loc_3:* = this._currentWinList.indexOf(_loc_2);
            this._currentWinList.splice(_loc_3, 1);
            this._windowList.push(_loc_2);
            return;
        }// end function

    }
}
